package com.tianpengtech.admin.feedback;
import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.admin.AbstractAdminController;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.InitInterceptor;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.model.feedback.FeedBack;
import com.tianpengtech.common.util.JsonUtil;

/**
* 路由配置地址：add("/admin/feedback", FeedBackAdminController.class, "/admin/feedback");
* 数据库映射地址：arp.addMapping(FeedBack.dao.tableName(),FeedBack.class);
*/
@Before(InitInterceptor.class)
@RequiresAuthentication
public class FeedBackAdminController extends AbstractAdminController{
	private IAdminOpt opt = new FeedBackAdminServiceImpl();
	@Override
	protected void config() {
		this.clazz=FeedBack.class;
		this.service=new FeedBackAdminServiceImpl();
	}

	@Override
	public void beforeAdd() {

	}

	@Override
	public void beforeAdd(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model< ? > model) {

	}

	@Override
	public void parsePageRecord(Object results) {
		FeedBack feedBack = (FeedBack)results;
		feedBack.set(FeedBack.STATUS, feedBack.status());
	}
	
	public void feedDetail(){
		int id = getParaToInt(FeedBack.ID, GlobalConfigConstant.DEFAULT_VALUE_ZERO);
		setAttr("feedBack", this.service.queryById(getModel(FeedBack.class), id));
		renderTemplate("feedback-detail.html");
	}
	
	/**
	 * 处理完成通过
	 */
	public void  markerAsFinished(){
		int id = getParaToInt(FeedBack.ID,0);
		FeedBack feedBack = getModel(FeedBack.class);
		try {
			opt.changeStatus(feedBack, id, FeedBack.STATUS_SUCCESS, Kv.create());
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}
}

	